package com.primb.bench.common.viewmodel;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.primb.bench.R;
import com.zhy.autolayout.AutoRelativeLayout;

/**
 * Created by Chen on 2017/5/10.
 * 功能描述：更多信息类型数据驱动模型
 */

public class ViewMoreInfoEntity implements Parcelable {
    String title;
    String moreInfo;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getMoreInfo() {
        return moreInfo;
    }

    public void setMoreInfo(String moreInfo) {
        this.moreInfo = moreInfo;
    }

    public void addView(Context context, ViewGroup viewGroup, View.OnClickListener listener) {
        AutoRelativeLayout layout = new AutoRelativeLayout(context);
        layout.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
        layout.setGravity(Gravity.CENTER_VERTICAL);

        ImageView ivIcon = new ImageView(context);
        AutoRelativeLayout.LayoutParams paramsIcon = new AutoRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        paramsIcon.addRule(AutoRelativeLayout.ALIGN_PARENT_LEFT);
        paramsIcon.addRule(AutoRelativeLayout.CENTER_VERTICAL);
        ivIcon.setId(R.id.iv_more_info);
        ivIcon.setImageResource(R.mipmap.moer_info_icon);
        ivIcon.setLayoutParams(paramsIcon);
        ivIcon.setPadding(40, 0, 0, 0);

        layout.addView(ivIcon);

        TextView tvKey = new TextView(context);
        tvKey.setGravity(Gravity.CENTER);
        tvKey.setTextSize(14);
        tvKey.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
        if (title != null) {
            tvKey.setText(title);
        }
        AutoRelativeLayout.LayoutParams paramsLeft = new AutoRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        paramsLeft.addRule(AutoRelativeLayout.CENTER_VERTICAL);
        paramsLeft.addRule(AutoRelativeLayout.RIGHT_OF, ivIcon.getId());
        tvKey.setPadding(40, 40, 20, 40);
        tvKey.setLayoutParams(paramsLeft);
        layout.addView(tvKey);

        ImageView ivArrow = new ImageView(context);
        AutoRelativeLayout.LayoutParams paramsArrow = new AutoRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        paramsArrow.addRule(AutoRelativeLayout.ALIGN_PARENT_RIGHT);
        paramsArrow.addRule(AutoRelativeLayout.CENTER_VERTICAL);
        ivArrow.setImageResource(R.mipmap.arraw_right);
        ivArrow.setLayoutParams(paramsArrow);
        ivArrow.setPadding(20, 40, 60, 40);

        layout.addView(ivArrow);

        viewGroup.addView(layout);
        View line = new View(context);
        line.setBackgroundColor(ContextCompat.getColor(context, R.color.gray_efefef));
        line.setAlpha(0);
        viewGroup.addView(line, new AutoRelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 10));

        if (moreInfo != null && !moreInfo.equals("")) {
            layout.setOnClickListener(listener);
        }
    }

    public ViewMoreInfoEntity() {
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.title);
        dest.writeString(this.moreInfo);
    }

    protected ViewMoreInfoEntity(Parcel in) {
        this.title = in.readString();
        this.moreInfo = in.readString();
    }

    public static final Creator<ViewMoreInfoEntity> CREATOR = new Creator<ViewMoreInfoEntity>() {
        @Override
        public ViewMoreInfoEntity createFromParcel(Parcel source) {
            return new ViewMoreInfoEntity(source);
        }

        @Override
        public ViewMoreInfoEntity[] newArray(int size) {
            return new ViewMoreInfoEntity[size];
        }
    };
}
